package com.education.welco.controller.app;


import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.education.common.pojo.LCatalog;
import com.education.common.utils.ApiReturnObject;
import com.education.common.utils.DateUtil;
import com.education.common.utils.DateUtils;
import com.education.welco.pojo.*;
import com.education.welco.service.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.awt.*;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.List;


@Api(tags = {"APP-平台课程详情"})
@RestController
@RequestMapping("/app/catalog")
public class AppLCatalogController {


    @Autowired
    private ILCatalogService catalogService;

    @Autowired
    private ITTeacherService teacherService;

    @Autowired
    private ILLabelService labelService;

    @Autowired
    private ILCatalogUserService catalogUserService;

    @Autowired
    private ILCatalogFootprintService ilCatalogFootprintService;

    @ApiOperation(value = "查询教学云课堂接口", httpMethod = "GET")
    @GetMapping("/selectList")
    public ApiReturnObject selectList(Integer pageNum, Integer pageSize, Integer catalogState,
                                      Integer catalogMenuId, Integer catalogId, Integer userId) {
        if (pageNum != null && pageSize != null) {
            PageHelper.startPage(pageNum, pageSize);
        }
        //catalogState  :  1 在授 2完结
        LCatalog catalog = new LCatalog();
        catalog.setCatalogState(catalogState);
        catalog.setParentId(catalogMenuId);
        catalog.setId(catalogId);
        List<LCatalog> catalogList = catalogService.selectLCatalogList(catalog);
        //查询该课程的标签和老师姓名
        selectTeacherByNameAndIntro(catalogList);
        //如果用户点击详情  添加为我的足迹
        if (catalogId != null) {
            if (userId == null) {
                return new ApiReturnObject("201", "userId为空", "userId为空");
            }
            insertFootPrint(userId, catalogId);
        }
        //是否添加到课程表 0 未添加  1 已添加
        isSchedule(userId, catalogList);
        PageInfo<LCatalog> pageInfo = new PageInfo<>(catalogList);
        return new ApiReturnObject("200", "查询成功", pageInfo);
    }


    @ApiOperation(value = "查询平台课程", httpMethod = "GET")
    @GetMapping("/selectAll")
    public ApiReturnObject selectList2(Integer pageNum, Integer pageSize,Integer userId, Integer catalogId) {
        if (pageNum != null && pageSize != null) {
            PageHelper.startPage(pageNum, pageSize);
        }
        if (userId == null) {
            return new ApiReturnObject("201", "userId为空", "userId为空");
        }
        List<LCatalog> catalogList = catalogService.findByUserId(userId, catalogId);
        selectTeacherByNameAndIntro(catalogList);
        //如果用户点击详情  添加为我的足迹
        if (catalogId != null) {
            insertFootPrint(userId, catalogId);
        }
        //是否添加到课程表 0 未添加  1 已添加
        isSchedule(userId, catalogList);

        PageInfo<LCatalog> pageInfo = new PageInfo<>(catalogList);
        return new ApiReturnObject("200", "查询成功",pageInfo);
    }

    @ApiOperation(value = "我的足迹接口", httpMethod = "GET")
    @GetMapping("/selectByFootPrint")
    private ApiReturnObject selectByFootPrint(Integer userId, Integer catalogId) {
        if (userId == null) {
            return new ApiReturnObject("201", "userId为空", "userId为空");
        }
        //获取当前日期前30天
        List<Date> list = DateUtil.diffDateList(new Date(), 30);
        //存放每天的足迹
        List<Map<String, Object>> mapList = new ArrayList<>();
        for (int i = 0; i < list.size(); i++) {
            Map<String, Object> map = new HashMap<>();
            //格式化日期
            String date = DateUtil.getDate(list.get(i));
            List<LCatalog> catalogList = catalogService.selectByFootPrint(userId, catalogId, date);
            //查询该课程的标签和老师姓名
            selectTeacherByNameAndIntro(catalogList);
            if (catalogList.size() != 0) {

                map.put("date", date);
                map.put("list", catalogList);
                mapList.add(map);
            }
            //是否添加到课程表 0 未添加  1 已添加
            isSchedule(userId, catalogList);
        }
        return new ApiReturnObject("200", "查询成功", mapList.size(), mapList);
    }

/*    @ApiOperation(value = "我的足迹接口", httpMethod = "GET")
    @GetMapping("/selectByFootPrint")
    private ApiReturnObject selectByFootPrint(Integer userId,Integer catalogId) {
        if (userId == null){
            return new ApiReturnObject("201", "userId为空", "userId为空");
        }
        List<LCatalog> catalogList = catalogService.selectByFootPrint(userId,catalogId);
        selectTeacherByNameAndIntro(catalogList);
        return new ApiReturnObject("200", "查询成功", catalogList.size(),catalogList);
    }*/

    /**
     * 如果用户点击详情  添加为我的足迹
     *
     * @param userId
     * @param catalogId
     */
    private void insertFootPrint(Integer userId, Integer catalogId) {
        //条件查询该课程的某个用户今天是否添加过足迹  添加过 则不添加
        LCatalogFootprint catalogFootprint = new LCatalogFootprint();
        catalogFootprint.setUserId(userId);
        catalogFootprint.setCatalogId(catalogId);
        String format = DateUtil.getDate(new Date());
        catalogFootprint.setNowCreateTime(format);
        List<LCatalogFootprint> catalogFootprintList = ilCatalogFootprintService.selectLCatalogFootprintList(catalogFootprint);
        if (catalogFootprintList.size() == 0) {
            LCatalogFootprint footprint = new LCatalogFootprint();
            footprint.setUserId(userId);
            footprint.setCatalogId(catalogId);
            footprint.setCreateTime(new Date());
            ilCatalogFootprintService.insertLCatalogFootprint(footprint);
        }
    }


    /**
     * 是否添加到课程表 0 未添加  1 已添加
     *
     * @param userId
     * @param catalogList
     */
    private void isSchedule(Integer userId, List<LCatalog> catalogList) {
        for (LCatalog lCatalog : catalogList) {
            LCatalogUser lCatalogUser = new LCatalogUser();
            lCatalogUser.setCatalogId(lCatalog.getId());
            lCatalogUser.setUserId(userId);
            List<LCatalogUser> lCatalogUserList = catalogUserService.selectLCatalogUserList(lCatalogUser);
            if (lCatalogUserList.size() == 0) {
                lCatalog.setSchedule("0");
            } else {
                lCatalog.setSchedule("1");
            }
        }
    }

    /**
     * 查询该课程的标签和老师姓名
     *
     * @param catalogList
     */
    private void selectTeacherByNameAndIntro(List<LCatalog> catalogList) {
        for (LCatalog lCatalog : catalogList) {
            //格式化日期  创建时间
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日");
            String format = null;
            if (lCatalog.getCreateTime() != null) {
                format = sdf.format(lCatalog.getCreateTime());
            }
            lCatalog.setSimpleCreateTime(format);
            //查询课程的老师信息
            /*if (lCatalog.getCatalogTeacherid() == null || "".equals(lCatalog.getCatalogTeacherid())) {
                lCatalog.setTeacherName("老师");//老师姓名
                lCatalog.setTeacherIntro("精英讲师");//老师简介
            } else {
                TTeacher tTeacher = teacherService.selectTTeacherById(Integer.parseInt(lCatalog.getCatalogTeacherid()));
                if (tTeacher == null || "".equals(tTeacher)) {
                    lCatalog.setTeacherName("老师");//老师姓名
                    lCatalog.setTeacherIntro("精英讲师");//老师简介
                } else {
                    lCatalog.setTeacherName(tTeacher.getTeacherName());//老师姓名
                    lCatalog.setTeacherIntro(tTeacher.getTeacherIntro());//老师简介
                }
            }*/
            lCatalog.setTeacherName(lCatalog.getCatalogTeacherName());//老师姓名
            lCatalog.setTeacherIntro(lCatalog.getCatalogTeacherIntro());//老师简介
            //查询课程标签
            List<Label> labelList = labelService.selectLLabelByCatalogId(lCatalog.getId());
            lCatalog.setLabelList(labelList);
        }
    }


    @ApiOperation(value = "平台课程--添加课程表", httpMethod = "POST")
    @PostMapping("/insertBySchedule")
    public ApiReturnObject insertBySchedule(Integer catalogId, Integer userId, Integer schedule) {
        if (catalogId == null || schedule == null) {
            return new ApiReturnObject("201", "参数缺失", "参数缺失");
        } else {
            //1 添加到课程表  0 取消添加
            if (schedule == 1) {
                //查询课程表是否有该课程
                List<LCatalogUser> lCatalogUsers = getlCatalogUsers(catalogId, userId);
                if (lCatalogUsers.size() > 0) {
                    return new ApiReturnObject("201", "课程已添加到课程表 不能重复添加", "课程已添加到课程表 不能重复添加");
                }
                //添加课程表
                LCatalogUser catalogUser = new LCatalogUser();
                catalogUser.setCatalogId(catalogId);
                catalogUser.setUserId(userId);
                int i = catalogUserService.insertLCatalogUser(catalogUser);
                Map<String, String> map = new HashMap<>();
                map.put("schedule", "1");
                return new ApiReturnObject("200", "添加成功", map);
            } else if (schedule == 0) {
                //查询课程表是否有该课程
                List<LCatalogUser> lCatalogUsers = getlCatalogUsers(catalogId, userId);
                if (lCatalogUsers.size() == 0) {
                    return new ApiReturnObject("202", "我的课程表没有该课程  无法取消", "我的课程表没有该课程  无法取消");
                }
                //取消添加
                int i = catalogUserService.deleteLCatalogUserByCatalogIdAndUserId(catalogId, userId);
                Map<String, String> map = new HashMap<>();
                map.put("schedule", "0");
                return new ApiReturnObject("200", "取消成功", map);
            } else {
                return new ApiReturnObject("203", "请输入正确的schedule参数 : 1 添加到课程表  2 取消添加", "请输入正确的schedule参数 : 1 添加到课程表  2 取消添加");
            }
        }
    }

    /**
     * 查询课程表是否有该课程
     *
     * @param catalogId
     * @param userId
     * @return
     */
    private List<LCatalogUser> getlCatalogUsers(Integer catalogId, Integer userId) {
        LCatalogUser lCatalogUser = new LCatalogUser();
        lCatalogUser.setUserId(userId);
        lCatalogUser.setCatalogId(catalogId);
        return catalogUserService.selectLCatalogUserList(lCatalogUser);
    }

}
